#include "matrix.h"
#include "GICv3.h"
#include "GICv3_gicc.h"
#include "memory.h"

/* the qemu-virt gicv3 gicd base address */
#define VIRT_GICV3_GICD_BASEASSR 0x8000000U
/* the qemu-virt gicv3 gicr base address */
#define VIRT_GICV3_GICR_BASEASSR 0x80a0000U
/* heap symbols from linker sccript */
extern mx_void_pt __heap_start;
extern mx_void_pt __heap_end;

extern mx_uint32_t GetAffinity();

static void interrupt_init ()
{
    Set_GICD_BaseAddr((void*)VIRT_GICV3_GICD_BASEASSR);
    Set_GICR_BaseAddr((void*)VIRT_GICV3_GICR_BASEASSR);

    /* enable system register | fiq bypass disabled | irq bypass disabled */
    setICC_SRE_EL1(sreDIB | sreDFB | sreSRE);
    WakeupGICR(GetGICR(GetAffinity()));
    setICC_PMR(0xff);
    SetSPISecurityAll(0x01); // !!

    /* gicdctlr_EnableGrp1A */
    EnableGICD(gicdctlr_EnableGrp1NS);

    setICC_IGRPEN1_EL1(igrpEnable);
}

static void board_init ()
{
    interrupt_init();
    /* initialize memory */ // FIXME: 调用它 MMU 代码不能正常工作
    // mx_memory_register("RAM1", &__heap_start, &__heap_end - &__heap_start);
}
MX_BOARDINIT_EXPORT(board_init, 0);
